package course.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import course.pojo.entity.Admin;
import course.service.AdminService;
import course.service.impl.AdminServiceImpl;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author HLF
 * @date 2022/7/2 19:09
 */
@WebServlet("/admin/adminController")
public class AdminController extends BaseController {
    private AdminService adminService = new AdminServiceImpl();


    /**
     * 管理员登录
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String auser = req.getParameter("auser");
        String apassword = req.getParameter("apassword");


        Admin byAuser = adminService.getByAuser(auser);
        boolean login = false;
        if (byAuser != null) {
            login = DigestUtil.bcryptCheck(apassword, byAuser.getApassword());
        }

        if (login) {
            req.getSession().setAttribute("auser", byAuser);
            req.getSession().setAttribute("role", "admin");//设置角色为管理员
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        } else {
            req.setAttribute("msg", "用户名或密码错误!");
            req.getRequestDispatcher("/admin_login.jsp").forward(req, resp);
        }
    }

    /**
     * 管理员退出登录
     */
    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("auser");
        req.getSession().removeAttribute("role");

        resp.sendRedirect(req.getContextPath() + "/index.jsp");
    }

    /**
     * 验证密码：
     * 1.原密码或新密码为空
     * 2.原密码错误
     * 3.两次密码不一致
     */
    protected void pass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();

        String apassword = req.getParameter("apassword");
        String apassword2 = req.getParameter("apassword2");
        String apassword3 = req.getParameter("apassword3");

        Map<String, Object> result = new HashMap<>();
        ObjectMapper mapper = new ObjectMapper();

        if (StringUtils.isBlank(apassword) || StringUtils.isBlank(apassword2) || StringUtils.isBlank(apassword3)) {
            result.put("code", 0);
            result.put("msg", "原密码或新密码不能为空");
            String s = mapper.writeValueAsString(result);
            writer.println(s);
            return;
        }

        if (!apassword2.equalsIgnoreCase(apassword3)) {
            result.put("code", 0);
            result.put("msg", "两次密码不一致！");
            String s = mapper.writeValueAsString(result);
            writer.println(s);
            return;
        }

        HttpSession session = req.getSession();
        Admin admin = (Admin) session.getAttribute("auser");
        boolean check = DigestUtil.bcryptCheck(apassword, admin.getApassword());
        if (!check) {
            result.put("code", 0);
            result.put("msg", "原密码错误！");
            String s = mapper.writeValueAsString(result);
            writer.println(s);
        }
    }
}
